﻿using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using System.Web.SessionState;
using BSPortal.Usercontrol.Admin.Book.ListBook;
using BSPortal.Usercontrol.Admin.Book.Writer;

namespace BSPortal
{
    public class ImageRequestHandler: IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.Clear();

            if(context.Request.QueryString.Count != 0)
            {
                var imagefor = context.Request.QueryString["imagefor"];
                switch (imagefor)
                {
                    case "book":
                        var storedImageBook = context.Session[Common.STORED_IMAGE] as byte[];
                        if (storedImageBook != null)
                        {
                            Image image = GetImage(storedImageBook);
                            if (image != null)
                            {
                                context.Response.ContentType = "image/jpeg";
                                image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                            }
                        }
                        break;
                    case "writer":
                        var storedImageWriter = context.Session[Common.STORED_IMAGE] as byte[];
                        if (storedImageWriter != null)
                        {
                            Image image = GetImage(storedImageWriter);
                            if (image != null)
                            {
                                context.Response.ContentType = "image/jpeg";
                                image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                            }
                        }
                        break;
                    case "publisher":
                        var storedImagePublisher = context.Session[Common.STORED_IMAGE] as byte[];
                        if (storedImagePublisher != null)
                        {
                            Image image = GetImage(storedImagePublisher);
                            if (image != null)
                            {
                                context.Response.ContentType = "image/jpeg";
                                image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                            }
                        }
                        break;
                    default:
                        var storedImage = context.Session[Common.STORED_IMAGE] as byte[];
                        if (storedImage != null)
                        {
                            Image image = GetImage(storedImage);
                            if (image != null)
                            {
                                context.Response.ContentType = "image/jpeg";
                                image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                            }
                        }
                        break;

                }
                
            }
        }

        private Image GetImage(byte[] storedImage)
        {
            var stream = new MemoryStream(storedImage);
            return Image.FromStream(stream);
        }

        public bool IsReusable
        {
            get { return false; }
        }
    }

}